/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConductoresPanel.java
 *
 * Created on 02-may-2011, 22:12:56
 */

package ia_psr.transportesGUI;

import ia_psr.problemas.Variable;
import ia_psr.problemas.transportes.Conductor;
import ia_psr.problemas.transportes.EmpresaTransportes;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;

/**
 *
 * @author Rubén Avalos Elvira y Fabiana Celiberti Tapie
 */
public class ConductoresPanel extends javax.swing.JPanel {

    /** Creates new form ConductoresPanel */
    public ConductoresPanel() {
        initComponents();
    }

    public void populateConductoresList() {
        EmpresaTransportes et = EmpresaTransportes.getInstance();
        DefaultListModel model = new DefaultListModel();
        for (Variable v : et.elementos) {
            Conductor c = (Conductor)v;
            model.addElement(c);
        }
        conductoresList.setModel(model);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        conductoresList = new javax.swing.JList();
        btnRemoveConductor = new javax.swing.JButton();
        btnAddConductor = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Conductores", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(102, 102, 102))); // NOI18N

        conductoresList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(conductoresList);

        btnRemoveConductor.setText("Eliminar");
        btnRemoveConductor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnRemoveConductorMouseReleased(evt);
            }
        });

        btnAddConductor.setText("Añadir");
        btnAddConductor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnAddConductorMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnAddConductor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnRemoveConductor))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnRemoveConductor)
                    .addComponent(btnAddConductor))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddConductorMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnAddConductorMouseReleased
        // TODO add your handling code here:
        showAddConductorDialog();
    }//GEN-LAST:event_btnAddConductorMouseReleased

    public void showAddConductorDialog() {
        JFrame frame = new AddConductorDialog(this);
        frame.setVisible(true);
    }

    private void btnRemoveConductorMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnRemoveConductorMouseReleased
        // TODO add your handling code here:
        removeSelectedConductor();

    }//GEN-LAST:event_btnRemoveConductorMouseReleased

    public void removeSelectedConductor() {
        try {
            int i = conductoresList.getSelectedIndex();
            Conductor c = (Conductor)conductoresList.getModel().getElementAt(i);
            EmpresaTransportes et = EmpresaTransportes.getInstance();
            et.removeConductor(c);

            DefaultListModel model = (DefaultListModel)conductoresList.getModel();
            model.removeElementAt(i);
        } catch (Exception e) {

        }
    }

    public void clearConductoresList() {
        if (conductoresList.getModel() instanceof DefaultListModel) {
            DefaultListModel model = (DefaultListModel)conductoresList.getModel();
            model.removeAllElements();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddConductor;
    private javax.swing.JButton btnRemoveConductor;
    private javax.swing.JList conductoresList;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
